package org.lanqiao.demo12.controller;

import org.lanqiao.demo12.domain.Userinfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;

@Controller
//@RequestMapping(value = "/Userinfo")
public class UserinfoController {
    /*
     * 处理登录
     **/


    @GetMapping("/login")
    @ResponseBody
    public Object login(@RequestParam(value = "username", required = true) String username,
                        @RequestParam(value = "password", required = false, defaultValue = "123456") String password) {
        HashMap map = new HashMap();
        if ("admin".equals(username) && "123456".equals(password)) {
            map.put("message", "登录成功");

        } else {
            map.put("message", "用户名或密码错误");
        }
        return map;
    }







    /**
     * fetch("http://localhost:3000/userinfo/reqister",{
     *     mothod:"post",
     *     body:JSON.stringify({
     *         username:'张三'
     *         password:123456,
     *         email:
     *     })
     * })
     *
     *
     *
     * */


    @PostMapping("/register")
    @ResponseBody
    public Object register(  Userinfo userinfo){
        System.out.println(userinfo.getUsername());
        HashMap map=new HashMap();
        map.put("message",userinfo);
        return map;
    }







    /**
     * 查询某个商品（根据主键查询）
     *
     * */

    @RequestMapping("/list")
    @ResponseBody
    public Object listFile() throws IOException{
        File file = new File("F:\\");
        FileInputStream fis =new FileInputStream(file);
        BufferedReader br =new BufferedReader(new InputStreamReader(fis));
        String line = br.readLine();
        return line;
    }


    @GetMapping("/goods/{id}")
    @ResponseBody
    public Object queryGoods(@PathVariable("id") String id){
        System.out.println("跳转过来的编号是："+id);
        //t0d0 查数据库
        ArrayList list =new ArrayList<>();
        list.add("木瓜多动");
        list.add("茉莉奶绿");
        list.add("咖啡拿铁");
        list.add("冰镇可乐");
        return list;
    }



}
